﻿#region License

// PentaBiz - Sustainable Software Development Framework Copyright (C) 2013 Zoltán Csizmazia
// 
// This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PentaBiz.Cloud.CloudDb.EntityModel.MetaModel;

namespace PentaBiz.Cloud.CloudDb.Testing
{
    [TestClass]
    public class UnitTestPocoChangeTracking
    {
        [ClassInitialize]
        public static void ClassInit(TestContext context)
        {
            using (var ctx = new MetaDbContext())
            {
                ctx.LoginAsRootService();
            }
        }

        [ClassCleanup]
        public static void Terminate()
        {
            using (var ctx = new MetaDbContext())
            {
                ctx.LoginAsRootService();

                List<MetaApplication> apps = ctx.MetaApplications.Where(ma => ma.Name.StartsWith("__testapp")).ToList();

                foreach (MetaApplication metaApplication in apps)
                {
                    ctx.MetaApplications.Remove(metaApplication);
                }

                ctx.SaveChanges();
            }
        }


        [TestMethod]
        [TestCategory("CloudMetaApplication")]
        public void TestMetaSampleApplication()
        {
            using (var ctx = new MetaDbContext())
            {
                ctx.LoginAsRootService();

                string name = "a" +
                              DateTime.Now.ToString().Replace(".", "_")
                                      .Replace("-", "_").Replace("\\", "_")
                                      .Replace("/", "_").Replace(":", "_").Replace(" ", "_");

                ctx.MetaApplications.Add(MetaApplication.CreateSampleApplication(name));

                ctx.SaveChanges();
            }
        }


        [TestMethod]
        [TestCategory("CloudMetaApplication")]
        public void TestMetaApplicationVersions()
        {
            string name = "a" +
                          DateTime.Now.ToString().Replace(".", "_")
                                  .Replace("-", "_").Replace("\\", "_")
                                  .Replace("/", "_").Replace(":", "_").Replace(" ", "_");

            Guid id = Guid.Empty;

            using (var ctx = new MetaDbContext())
            {
                ctx.LoginAsRootService();

                MetaApplication metaApp = MetaApplication.CreateSampleApplication(name);

                ctx.MetaApplications.Add(metaApp);

                ctx.SaveChanges();

                id = metaApp.Id;
            }

            using (var ctx = new MetaDbContext())
            {
                ctx.LoginAsRootService();

                MetaApplication metaApp = ctx.MetaApplications.First(ma => ma.Id == id);

                Assert.IsTrue(new MetaVersion("1.0.0.0").Equals(metaApp.MetaVersion),
                              "MetaVersion failed expected:{0}, actual:{1}", new MetaVersion("1.0.0.0"),
                              metaApp.MetaVersion);
                Assert.IsTrue(metaApp.MetaChangeSets.Count > 0);

                Assert.IsNotNull(metaApp.GetWorkingMetaChangeSet());
                Assert.IsTrue(metaApp.GetWorkingMetaChangeSet().MetaApplicationChangeItems.Count > 0);
            }
        }


        [TestMethod]
        [TestCategory("CloudMetaDbContext")]
        public void TestPocoGraphTrackingPerformance()
        {
            using (var ctx = new MetaDbContext())
            {
                ctx.LoginAsRootService();
                for (int i = 0; i < 10000; i++)
                {
                    var metaApp = new MetaApplication {Name = string.Format("__testapp{0}", i)};

                    metaApp.AddNewMetaDataTable(t =>
                        {
                            t.Name = "Apple";
                            t.EntitySetName = "Apples";
                        });

                    metaApp.IsDeleted = true;
                    ctx.MetaApplications.Add(metaApp);
                }

                ctx.SaveChanges();
            }
        }
    }
}